package panel.shopDetail
{
	import assists.ButtonUtils;
	import assists.HtmlText;
	import assists.Lang;
	import assists.TextFieldUtils;
	import assists.URI;
	import assists.resource.ImageResource;
	import assists.resource.ResourceLoader;
	
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	
	import inerface.IShopDetailPanel;
	
	import main.ClientConstants;
	
	import panel.eating.DragContainerBox;
	import panel.eating.GetMoreBox;
	import panel.eating.RankBtnGroupList;
	import panel.stars.StarGroup;
	
	import struct.Restaurant;
	
	public class ShopDetailPanel extends Sprite implements IShopDetailPanel
	{
		private var _stage:Stage;
		private var _loader:ResourceLoader;
		private var _lang:Lang;
		private var _uri:URI;
		
		//界面组件
		private var mainIcon:Sprite;
		private var titleText:TextField;
		private var starLevel:StarGroup;
		private var infoText:TextField;
		private var btnGroup:RankBtnGroupList;
		private var dragContainer:DragContainerBox;
		private var costText:TextField;
		
		private var line1:Shape;
		private var line2:Shape;
		
		private var shop:Restaurant;
		private var img:ImageResource;
		
		private var commentBoxList:Array = [];
		
		private var _onComment:Function;
		private var _onAddPath:Function;
		private var _onShowLocation:Function;
		
		public function ShopDetailPanel()
		{
		}
		
		public function init(stage:Stage,loader:ResourceLoader,lang:Lang,uri:URI = null):void
		{
			_stage = stage;
			_loader = loader;
			_lang = lang;
			_uri = uri;
			
			initThis();
		}
		
		private function initThis():void
		{
			//Icon
			mainIcon = new Sprite;
			mainIcon.graphics.lineStyle(1,0x000000,.5);
			mainIcon.graphics.beginFill(0x222222,.5);
			mainIcon.graphics.drawRoundRect(0,0,_stage.stageHeight * .15,_stage.stageHeight * .15,5);
			mainIcon.graphics.endFill();
			
			this.addChild(mainIcon);
			mainIcon.x = _stage.stageHeight * .02;
			mainIcon.y = _stage.stageHeight * ClientConstants.HOME_UP_PER + _stage.stageHeight * .01;
			
			//title
			titleText = new TextField;
			titleText.wordWrap = false;
			titleText.multiline = false;
			titleText.mouseEnabled = false;
			this.addChild(titleText);
			
			//star
			starLevel = new StarGroup(StarGroup.READ_STARS);
			starLevel.scaleX = 1;
			starLevel.scaleY = 1;
			this.addChild(starLevel);
			
			//特色文本
			infoText = new TextField();
			infoText.wordWrap = true;
			infoText.multiline = true;
			infoText.mouseEnabled = false;
			infoText.width = _stage.stageWidth * .96;
			this.addChild(infoText);
			
			//分割线1
			line1 = new Shape;
			line1.graphics.lineStyle(1,0x000000,.8);
			line1.graphics.lineTo(_stage.stageWidth,0);
			this.addChild(line1);
			line1.x = 0;
			line1.y = _stage.stageHeight * ClientConstants.HOME_UP_PER + _stage.stageHeight * .17;
			
			//分割线2
			line2 = new Shape;
			line2.graphics.lineStyle(1,0x000000,.8);
			line2.graphics.lineTo(_stage.stageWidth,0);
			this.addChild(line2);
			line2.x = 0;
			line2.y = _stage.stageHeight * ClientConstants.HOME_UP_PER + _stage.stageHeight * .3;
			
			//订餐导航路线添加Btn
			btnGroup = new RankBtnGroupList(_stage,_loader,_uri,_stage.stageWidth,_stage.stageHeight * .08);
			this.addChild(btnGroup);
			btnGroup.x = 0;
			btnGroup.y = line2.y;
			
			initBtnList();
			
			//人均消费
			costText = new TextField();
			costText.wordWrap = false;
			costText.multiline= false;
			costText.mouseEnabled =false;
			costText.autoSize = TextFieldAutoSize.RIGHT;
			this.addChild(costText);
			
			//评论区
			dragContainer = new DragContainerBox(_stage.stageWidth,_stage.stageHeight * .44,new GetMoreBox(_stage,_loader,_lang,getMoreInfo));
			this.addChild(dragContainer);
			dragContainer.x= 0;
			dragContainer.y = btnGroup.y + btnGroup.height;
		}
		
		private function initBtnList():void
		{
			var list:Array = [HtmlText.formatSizeColor("<b>查看位置</b>",0xFFFF00,20),HtmlText.formatSizeColor("<b>我要评价</b>",0xFFFF00,20),HtmlText.formatSizeColor("<b>开始预定</b>",0xFFFF00,20),HtmlText.formatSizeColor("<b>添加路线</b>",0xFFFF00,20)];
			
			function newShape():Shape
			{
				var btnShape:Shape = new Shape;
				btnShape.graphics.lineStyle(1,0x333333,.5);
				btnShape.graphics.beginFill(0x000000,.5);
				btnShape.graphics.drawRoundRect(0,0,_stage.stageWidth / list.length,_stage.stageHeight * .08,5);
				btnShape.graphics.endFill();
				return btnShape;
			}
			
			var btn1:ButtonUtils = new ButtonUtils(newShape(),showLocation,list[0]);
			var btn2:ButtonUtils = new ButtonUtils(newShape(),commentThisShop,list[1]);
			var btn3:ButtonUtils = new ButtonUtils(newShape(),bookThisShop,list[2]);
			var btn4:ButtonUtils = new ButtonUtils(newShape(),addToPath,list[3]);
			
			var btnList:Array = [btn1,btn2,btn3,btn4];
			btnGroup.changeData(btnList);
		}
		
		public function changeData(shopInfo:Object):void
		{
			if(shopInfo){
				shop = shopInfo as Restaurant;
				
				TextFieldUtils.setHtmlText(titleText,"<b>" + HtmlText.formatSizeColor(shopInfo.name,0xFFFF00,20) + "</b>",true,true);
				titleText.width = titleText.textWidth + 5;
				titleText.height = titleText.textHeight + 5;
				titleText.y = _stage.stageHeight * ClientConstants.HOME_UP_PER + _stage.stageHeight * .01;
				titleText.x = _stage.stageHeight * .025 + mainIcon.width;
				
				TextFieldUtils.setHtmlText(infoText,"<b>" + HtmlText.formatSizeColor(shopInfo.desc,0xDDDD00) + "</b>",true,true);
				infoText.height = infoText.textHeight + 5;
				infoText.x = _stage.stageHeight * .02;
				infoText.y = line1.y + 1;
				
				clearImage();
				img = new ImageResource(_loader,shopInfo.url,repositionImg);
				mainIcon.addChild(img);
				repositionImg();
				
				starLevel.starLevel = shopInfo.starLevel;
				starLevel.y = mainIcon.y + mainIcon.height / 2;
				starLevel.x = _stage.stageHeight * .025 + mainIcon.width;
				
				TextFieldUtils.setHtmlText(costText,HtmlText.formatSizeColor(_lang.client("人均消费") + shopInfo.costNum,0xffffff,16),true,true);
				costText.width = costText.textWidth + 5;
				costText.x = _stage.stageWidth - costText.width - _stage.stageWidth * .02
				costText.y = starLevel.y;
				
				function repositionImg():void
				{
					img.width = _stage.stageHeight * .15;
					img.height = _stage.stageHeight * .15;
					
					img.x = (_stage.stageHeight * .15 - img.width) / 2;
					img.y = (_stage.stageHeight * .15 - img.height) / 2;
				}
				
				addComment();
				
				dragContainer.changeData(commentBoxList);
			}
		}
		
		private function addComment():void
		{
			commentBoxList = [];
			
//			var box:CommentBox;
//			for(var i:int = 0;i < shop.commentList.length;i++){
//				box = new CommentBox(_stage,_loader,_lang,_uri,_stage.stageWidth,_stage.stageHeight * .18);
//				box.changeData(shop.commentList[i]);
//				
//				commentBoxList.push(box);
//			}
		}		
		
		private function clearImage():void
		{
			if(img != null){
				mainIcon.removeChild(img);
				img = null;
			}
		}
		
		private function getMoreInfo():void
		{
			trace("哈哈哈哈");
		}
		
		public function clear():void
		{
			btnGroup.clear();
		}
		
		private function addToPath():void
		{
			if(_onAddPath is Function){
				_onAddPath(shop);
			}
		}
		
		private function bookThisShop():void
		{
			
		}
		
		private function showLocation():void
		{
			if(_onShowLocation is Function){
				_onShowLocation(shop);
			}
		}
		
		private function commentThisShop():void
		{
			if(_onComment is Function){
				_onComment();
			}
		}
		
		public function set comment(func:Function):void
		{
			_onComment = func;
		}
		
		public function set onAddPath(func:Function):void
		{
			_onAddPath = func;
		}
		
		public function set onShowLocation(func:Function):void
		{
			_onShowLocation = func;
		}
	}
}